package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.qmyx.admin.config.CommonPropertiesConfig;
import com.ytjj.qmyx.admin.dao.FrontCategoryDao;
import com.ytjj.qmyx.admin.dao.ProductCategoryDao;
import com.ytjj.qmyx.admin.mapper.FrontCategoryMapper;
import com.ytjj.qmyx.admin.model.FrontCategory;
import com.ytjj.qmyx.admin.model.request.FrontCategoryRequest;
import com.ytjj.qmyx.admin.model.response.FrontCategoryResponse;
import com.ytjj.qmyx.admin.service.FrontCategoryService;
import com.ytjj.qmyx.mall.model.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 前台商品分类管理
 */
@RestController
@RequestMapping("/frontCategory")
public class FrontCategoryController {

    @Autowired
    private FrontCategoryService frontCategoryService;

    @Autowired
    CommonPropertiesConfig commonPropertiesConfig;

    @Autowired
    FrontCategoryDao frontCategoryDao;


    @Resource(name = "adminFrontCategoryMapper")
    FrontCategoryMapper frontCategoryMapper;

    @Autowired
    ProductCategoryDao productCategoryDao;

    /**
     * 根据当前Id查询子分类列表
     */
    @PostMapping("/getFrontByPid")
    public CommonResult getCategoryByPid(@RequestBody FrontCategory frontCategory){
        List<FrontCategory> frontCategories = frontCategoryService.getCategoryByPid(frontCategory);
        return CommonResult.success(frontCategories);
    }

    /**
     * 根据当前pid获取分类详情
     */
    @PostMapping("/getFrontDetailByPid")
    public CommonResult getFrontDetailByPid(@RequestBody FrontCategoryRequest frontCategoryRequest){
        FrontCategoryResponse frontCategories = frontCategoryService.getCategoryDetailById(frontCategoryRequest);
        return CommonResult.success(frontCategories);
    }


    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteCategory")
    public CommonResult deleteBottomCategory(@RequestParam("id") Integer id) {
        int count =  frontCategoryService.delById(id);
        return count > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed();
    }

    /**
     * 新增或修改前台分类
     */
    @PostMapping("/addOrEditFrontCategory")
    public CommonResult addOrEditFrontCategory(@RequestBody FrontCategoryRequest frontCategoryRequest){
        int count = frontCategoryService.addOrEditFrontCategory(frontCategoryRequest);
        return count > 0 ? CommonResult.success() : CommonResult.failed();
    }

    @GetMapping("getCategoryForFront/{id}")
    public CommonResult getCategoryForFront(@PathVariable Integer id) {
        return CommonResult.success(frontCategoryService.getCategoryForFront(id));
    }

    @GetMapping("setIsHidden")
    public CommonResult setIsHidden(@RequestParam("id")Integer id,Integer status) {
        return CommonResult.success(frontCategoryService.setIsHidden(id,status));
    }

    @GetMapping("test")
    public CommonResult test() {
//        int a =  frontCategoryDao.alterInc();
//        FrontCategory frontCategory = new FrontCategory();
//        frontCategory.setTitle("我是测试接口");
//        frontCategoryMapper.insert(frontCategory);
        List<ProductCategory> productCategories = new ArrayList<>();
        int id = 11000;
        for (int i=0; i<10; i++){
            ProductCategory productCategory = new ProductCategory();
            id += i;
            productCategory.setId(id);
            productCategory.setStatus((byte)1);
            productCategory.setPid(0);
            productCategory.setCreateTime(new Date());
            productCategory.setUpdateTime(new Date());
            productCategory.setTitle("我是测试1432");
            productCategory.setIcon(null);
            productCategories.add(productCategory);
        }
        int a = productCategoryDao.insertBatch(productCategories);
        return CommonResult.success(a);
    }



}
